package com.gkj.blog.controller;

import com.github.pagehelper.PageInfo;
import com.gkj.blog.business.enums.ArticleStatusEnum;
import com.gkj.blog.business.service.ArticleLookService;
import com.gkj.blog.business.service.ArticleService;
import com.gkj.blog.business.vo.condition.ArticleConditionVO;
import com.gkj.blog.business.vo.entity.ArticleLookVo;
import com.gkj.blog.business.vo.entity.ArticleVo;
import com.gkj.blog.business.vo.entity.User;
import com.gkj.blog.framework.holder.RequestHolder;
import com.gkj.blog.util.IpUtil;
import com.gkj.blog.util.RedisUtil;
import com.gkj.blog.util.ResultUtil;
import com.gkj.blog.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RenderController {
    @Autowired
    ArticleService articleService;
    @Autowired
    ArticleLookService articleLookService;
    @Autowired
    RedisUtil redisUtil;


    @GetMapping("/article/{articleId}.html")
    public ModelAndView lookArticle(@PathVariable("articleId") Long articleId){
        ArticleVo articleVo = articleService.select(articleId);
        String ip= IpUtil.getRealIp(RequestHolder.getRequest());
        if(articleVo!=null&&redisUtil.get(ip+articleVo.getId())==null) {
            redisUtil.set(ip+articleVo.getId(),true,600L);//10分钟存在时间，10分钟内多次浏览只插入一次浏览记录
            ArticleLookVo articleLookVo = new ArticleLookVo();
            articleLookVo.setLookTime(new Date());
            User user = SessionUtil.getUser();
            if(user!=null){
                articleLookVo.setUserId(user.getId());
            }
            articleLookVo.setArticleId(articleId);
            articleLookVo.setUserIp(ip);
            articleLookService.insert(articleLookVo);
        }
        Map<String ,Object> map =new LinkedHashMap();
        map.put("article",articleVo);
        List<ArticleVo> relatedArticleVos = articleService.listRelatedArticle(10, articleId);
        Map preAndNext=articleService.getPrevAndNextArticles(articleId);
        map.put("relatives",relatedArticleVos);
        map.put("preAndNext",preAndNext);
        return ResultUtil.view("article",map);
    }


    @RequestMapping("/list.html")
    public ModelAndView list(){
        return ResultUtil.view("list");
    }
    @RequestMapping("/life.html")
    public ModelAndView life(){
        return ResultUtil.view("life");
    }
    @RequestMapping("/index.html")
    public ModelAndView index(){
        return ResultUtil.view("index");
    }
    @GetMapping("/")
    public ModelAndView index2(){
        return ResultUtil.view("index");
    }
    @RequestMapping("/board.html")
    public ModelAndView board(){
        return ResultUtil.view("board");
    }
    @RequestMapping("/aboutMe.html")
    public ModelAndView aboutMe(){

        return ResultUtil.view("aboutMe");
    }

}
